<?php

/*
 * Author: Quan Van Sinh
 * Email: qvsinh@yahoo.com
 */

session_start();
error_reporting(E_ALL | E_STRICT);
error_reporting(1);
include "init.php";

$action = $_REQUEST['action'];

switch ($action) {
    case 'get_district':
        include_once MODEL_DIR . 'geographic.class.php';
        $geo = new geographic();
        $id = intval($_POST['city_id']);
        $rs = $geo->getByParentId($id);
        $html = '<option value="0">Chọn quận/huyện của bạn...</option>';
        while ($row = $db->fetch_array($rs)) {
            $html .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
        }
        echo $html;
        break;
    case 'delete_money':
        include MODEL_DIR . 'money.class.php';
        $objMoney = new money();
        $id = intval($_POST['id']);
        if ($id) {
            echo $objMoney->delete_money($id);
        }
        break;
    case 'register':
        include MODEL_DIR . 'validate.class.php';
        $validate = new validate();
        $first_name = trim($_POST['first_name']);
        $last_name = trim($_POST['last_name']);
        $email = $_POST['email'];
        $password = md5($_POST['password']);
        $re_password = md5($_POST['re_password']);
        $city = intval($_POST['city']);
        $district = intval($_POST['district']);

        if (empty($first_name) || $first_name == '---Họ & Tên đệm---' || empty($last_name) || $last_name == '---Tên---') {
            echo 'fullname_empty';
            die;
        }

        if (!$validate->email($email)) {
            echo 'email_error';
            die;
        } else {
            $sqlCheck = "SELECT * FROM member WHERE email='$email'";
            $rsCheck = $db->query_first($sqlCheck);
            if ($rsCheck['id']) {
                echo 'email_exist';
                die;
            }
        }

        if (strlen($_POST['password']) < 6) {
            echo 'password_short';
            die;
        }
        if (empty($_POST['password'])) {
            echo 'password_empty';
            die;
        }
        if ($password != $re_password) {
            echo 'password_not_match';
            die;
        }
        if ($district <= 0) {
            echo 'not_choose_address';
            die;
        }

        $create_date = date('Y-m-d h:i:s');
        $data = array();
        $data['first_name'] = $first_name;
        $data['last_name'] = $last_name;
        $data['email'] = $email;
        $data['password'] = $password;
        $data['geo_id'] = $district;
        $data['create_date'] = $create_date;
        $db->exec_insert('member', $data);
        echo 'ok';
        break;
}
?>
